package com.spring.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Aspect//注解通知
@Component
public class LogAspect {

    @Pointcut("execution(* com.spring.service.impl.*.*(..))")//切面类
    public void pt() {
    }

    ;

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) {

        String className = pjp.getTarget().getClass().getName();
        String method = pjp.getSignature().getName();
        Object result = null;
        try {
            System.out.println("进入" + className + "的" + method + "方法");
            long start = System.currentTimeMillis();

            // 执行目标方法
            result = pjp.proceed(pjp.getArgs());

            System.out.println(className + "的" + method + "方法执行完毕，耗时" + (System.currentTimeMillis() - start) + "ms");
            System.out.println("返回值：" + result);
        } catch (Throwable e) {
            System.out.println(className + "的" + method + "方法抛出异常：" + e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println(className + "的" + method + "方法结束");
            return result;
        }

    }
}